<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ProjectHire extends Model
{
    protected  $table='user_project';

    protected $fillable = [
        'user_id	',
        'project_id',
        'reward_time',
        'verify_status',
        'verify_msg'
    ];

    public function getExtendsAttribute() {
        if (isset($this->attributes['extends'])) {
            try {
                $this->attributes['extends'] = json_decode($this->attributes['extends']);
            }catch (\Exception $e) {

            }
//            dd($this->attributes['extends']);
            return $this->attributes['extends'];
        }

        return null;
    }

    public function setExtendsAttribute($value) {
        if (!empty($value)) {
            $value = json_encode($value);
        }

        $this->attributes['extends'] = $value;
    }

    public function getPaymentAttribute() {
        if (isset($this->attributes['payment'])) {
            try {
                $this->attributes['payment'] = json_decode($this->attributes['payment']);
            }catch (\Exception $e) {

            }
//            dd($this->attributes['extends']);
            return $this->attributes['payment'];
        }

        return null;
    }

    public function setPaymentAttribute($value) {
        if (!empty($value)) {
            $value = json_encode($value);
        }

        $this->attributes['payment'] = $value;
    }

    public function project() {
        return $this->belongsTo('App\Models\Project', 'project_id', 'id');
    }

    public function user() {
        return $this->belongsTo('App\User', 'user_id', 'id');
    }

    public function volunteer() {
        return $this->belongsTo('App\Models\Volunteer', 'user_id', 'user_id');
    }

    public function plusTime($time) {
        if (!$this->id) {
            return false;
        }

        $this->fill([
            'reward_time' => $this->reward_time + $time
        ]);

        return $this->save();
    }
}
